package com.avicsafety.lib.CustomView;

import android.widget.RelativeLayout;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.avicsafety.lib.R.id;
import com.avicsafety.lib.R.layout;
import com.avicsafety.lib.R.styleable;
import com.avicsafety.lib.tools.DateTime;
import com.avicsafety.lib.tools.DateTime.Format;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
/**
 * 功能：<年月日控件，只显示年月日>
 * Created by whb on 2017/11/17.
 */

public class AvicDateViewNew extends RelativeLayout implements I_kv {
    private TextView view_title;
    private TextView view_value;
    private View line;
    private String value;
    private String text;
    private DatePickerDialog dateDialog;
    private static AvicDateViewNew.OnDateSetListener mOnDateSetListener;

    public AvicDateViewNew(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(layout.s_item, this);
        TypedArray a = context.obtainStyledAttributes(attrs, styleable.AvicView);
        String lable = a.getString(styleable.AvicView_lable);
        String value = a.getString(styleable.AvicView_value);
        Boolean show_line = Boolean.valueOf(a.getBoolean(styleable.AvicView_show_line, true));
        this.view_title = (TextView)this.findViewById(id.view_title);
        this.view_value = (TextView)this.findViewById(id.view_value);
        this.line = this.findViewById(id.line);
        if(lable != null) {
            this.view_title.setText(lable);
        }

        if(value != null) {
            this.text = value;
        }

        view_value.setText(value);
        if(!show_line.booleanValue()) {
            line.setVisibility(View.GONE);
        }

        if(value == null || value.equals("") || value.trim().equals("")) {
            this.view_value.setText("");
        }

        dateDialog = new DatePickerDialog(context, (android.app.DatePickerDialog.OnDateSetListener)null, DateTime.getYear(), DateTime.getMonth() - 1, DateTime.getDay());
        dateDialog.setCancelable(true);
        dateDialog.setButton(-3, "清空", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                view_value.setText("");
                if(mOnDateSetListener != null) {
                    mOnDateSetListener.OnDateSet(AvicDateViewNew.this.dateDialog, AvicDateViewNew.this.getText());
                }

            }
        });
        this.dateDialog.setButton(-1, "确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                int year = AvicDateViewNew.this.dateDialog.getDatePicker().getYear();
                int monthOfYear = AvicDateViewNew.this.dateDialog.getDatePicker().getMonth();
                int dayOfMonth = AvicDateViewNew.this.dateDialog.getDatePicker().getDayOfMonth();
                String date = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                Date da = DateTime.getDate(Format.yyyymd, date);
                date = DateTime.getStrDate(Format.full, da);
                setText(DateTime.getStrDate(Format.yyyymmdd, da));
                setValue(date);
                if(mOnDateSetListener != null) {
                    mOnDateSetListener.OnDateSet(dateDialog, getText());
                }

            }
        });
        this.setOnClickListener(new android.view.View.OnClickListener() {
            public void onClick(View v) {
                dateDialog.show();
            }
        });
    }

    public void setLable(String title) {
        this.view_title.setText(title);
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLable() {
        return this.view_title.getText().toString();
    }

    public String getValue() {
        return this.value != null && this.value.length() != 0?this.value:(StringUtils.isEmpty(this.text)?"":this.text);
    }

    public String getText() {
        return StringUtils.isEmpty(this.text)?"":this.text;
    }

    public static void setOnDateSetListener(OnDateSetListener onDateSetListener) {
        mOnDateSetListener = onDateSetListener;
    }

    public void setText(String _text) {
        this.text = _text;
        if(!StringUtils.isEmpty(_text)) {
            this.view_value.setText(_text);
        } else {
            this.view_value.setText("");
        }

    }

    public interface OnDateSetListener {
        void OnDateSet(AlertDialog var1, String var2);
    }
}
